package net.skystudio.es.common;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * @author nicky
 * @Description: 分页对象
 * @date 2018-12-12
 **/
public class PageBean<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    private final static ThreadLocal<PageBean> pageBean = new ThreadLocal<PageBean>();

    //总共的数据条数
    private long totalRow;
    //第几页
    private long pageNum;
    //每页显示多少
    private long pageSize;
    //查询出来的数据
    private List<T> datas = new ArrayList<>();
    //总页数
    private long totalPage;

    // 扩展字段（热搜词）
    private List<String> hotWords;

    /**
     * 默认构造方法
     */
    public PageBean() {
        pageBean.set(this);
    }

    /**
     * 设置pageBean的参数
     */
    public void setParameters(long totalRow) {
        this.totalRow = totalRow;
        this.totalPage = (int) (totalRow % pageSize == 0 ? totalRow / pageSize
                : totalRow / pageSize + 1);
    }

    /**
     * 获取当前线程ThreadLocal变量绑定的pageBean
     */
    public static PageBean get() {
        return null != pageBean.get() ? pageBean.get() : new PageBean();
    }

    /**
     * 清除当前线程ThreadLocal变量
     */
    public static void clear() {
        pageBean.remove();
    }

    public long getTotalRow() {
        return totalRow;
    }

    public void setTotalRow(long totalRow) {
        this.totalRow = totalRow;
    }

    public long getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(long totalPage) {
        this.totalPage = totalPage;
    }

    public long getPageNum() {
        return pageNum;
    }

    public void setPageNum(long pageNum) {
        this.pageNum = pageNum;
    }

    public long getPageSize() {
        return pageSize;
    }

    public void setPageSize(long pageSize) {
        this.pageSize = pageSize;
    }

    public List<T> getDatas() {
        return datas;
    }

    public void setDatas(List<T> datas) {
        this.datas = datas;
    }

    public List<String> getHotWords() {
        return hotWords;
    }

    public void setHotWords(List<String> hotWords) {
        this.hotWords = hotWords;
    }

    public PageBean(long pageNum, long pageSize){
        this.pageNum = pageNum;
        this.pageSize = pageSize;
        pageBean.set(this);
    }
}
